package top.roadvast.sort.sort_obj;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ComparatorDemo {
    public void demoForComparator() {
        SweetHeart love1 = new SweetHeart("猪可爱", "pig");
        SweetHeart love2 = new SweetHeart("哈士奇", "dog");
        SweetHeart love3 = new SweetHeart("大火鸡", "chicken");
        ArrayList<SweetHeart> loveList = new ArrayList<>();
        loveList.add(love1);
        loveList.add(love2);
        loveList.add(love3);
//        Collections.sort(loveList, new Comparator<SweetHeart>() {
//                    @Override
//                    public int compare(SweetHeart sweet1, SweetHeart sweet2) {
//                        return sweet2.type.compareTo(sweet1.type);
//                    }
//                }
//        );
        //lambda表达式
        Collections.sort(loveList, (sweet1, sweet2) -> {
            return sweet2.type.compareTo(sweet1.type);
        });
        System.out.println("按类型首字母排序(降序)后结果如下");
        for (SweetHeart love : loveList) {
            System.out.println(love.toString());
        }
    }

    public static void main(String[] args) {
        ComparatorDemo obj = new ComparatorDemo();
        obj.demoForComparator();
    }

    class SweetHeart {
        String name;
        String type;

        public SweetHeart(String name, String type) {
            this.name = name;
            this.type = type;
        }

        @Override
        public String toString() {
            return "name is " + this.name + "; type is " + this.type;
        }
    }

}